package jwebtester.business.beans.competencies;

import javax.annotation.PostConstruct;

import jwebtester.business.services.SessionMapService;
import jwebtester.business.utils.Dialogs;
import jwebtester.business.utils.DialogsHelper;
import jwebtester.business.utils.FacesHelper;
import jwebtester.dal.provider.CompetenciesDataProvider;
import infrastructure.Scopes;

import org.primefaces.event.SelectEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import domain.entities.Competency;

@Component
@Scope(Scopes.Request)
public class NewCompetencyBean {

	private static final Logger LOG = LoggerFactory.getLogger(NewCompetencyBean.class);

	@Autowired
	private transient CompetenciesDataProvider dataProvider;

	@Autowired
	private transient SessionMapService sessionService;

	private Competency competency;

	private Competency parentCompetency;

	public NewCompetencyBean() {
		competency = new Competency();
		parentCompetency = new Competency();
	}

	@PostConstruct
	public void postConstruct() {
		competency.setOrganizationID(sessionService.getCurrentOrganizationID());
	}

	public Competency getCompetency() {
		return competency;
	}

	public void setCompetency(final Competency competency) {
		this.competency = competency;
	}

	public Competency getParentCompetency() {
		return parentCompetency;
	}

	public void setParentCompetency(final Competency parentCompetency) {
		this.parentCompetency = parentCompetency;
	}

	/**
	 * Shows new competency dialog and optionally notifies that parent
	 * competency isn't set
	 * 
	 * @param parent
	 *            Parent competency (may be null)
	 * @param showNotification
	 *            Flag specifies whether notification message will be show in
	 *            case of null parent
	 */
	public void beforeCreateNewCompetency(final Competency parent, final boolean showNotification) {
		if (parent == null && showNotification) {
			new FacesHelper().info("Info", "Parent competency hasn't been selected. "
					+ "New competency will be root one.");
		} else {
			parentCompetency = parent;
		}

		new DialogsHelper().showDialog(Dialogs.NEW_COMPETENCY);
	}
	
	public void handleSelectParent(final SelectEvent event){
		final Object selectedObject = event.getObject();
		if (selectedObject != null) {
			parentCompetency = (Competency) selectedObject;
		}
	}

	public void createNewCompetency() {
		LOG.trace("Save new competency has been clicked");

		if (competency != null) {
			LOG.trace(String.format("Name=%s, Description=%s, IsGroup=%s", competency.getName(),
					competency.getDescription(), competency.getIsGroup()));

			if ( parentCompetency != null && !parentCompetency.isNew()) {
				final int parentID = parentCompetency.getId();
				competency.setParentID(parentID);
			}

			try {
				dataProvider.createCompetency(competency);
				new FacesHelper().info("Success", "New competency has been successfully saved");
			} catch (Exception e) {
				LOG.error(String.format("Error saving new competency"), e);
				new FacesHelper().error("Error", "Error saving new competency");
			}
		}
	}
}
